package com.zj.leetcode.medium;

import java.util.Arrays;

/**
 * @program: algorithm
 * @description: 最长特殊序列 II
 * 522 longest-uncommon-subsequence-ii
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class LongestUncommonSubsequenceIi {
    public static void main(String[] args) {
//        String[] strs = new String[]{"abaf", "abad", "erf", "abaf", "er", "ab"};
//        String[] strs = new String[]{"aba","cdc","eae"};
//        String[] strs = new String[]{"aaa", "aaa", "aa"};
//        String[] strs = new String[]{"aaa", "aaa", "nn"};
        String[] strs = new String[]{"aaa","acb"};

        Solution solution = new LongestUncommonSubsequenceIi().new Solution();
        System.out.println(solution.findLUSlength(strs));

//        System.out.println(solution.haveThis(strs[1], strs[2]));
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int findLUSlength(String[] strs) {

            Arrays.sort(strs, (o1, o2) -> {
                if (o1.equals(o2)) {
                    return 0;
                }
                if (o1.length() > o2.length()) {
                    return -1;
                } else {
                    return 1;
                }
            });

            int i = 0;
            int j = 0;
            int ret = -1;
            while (i < strs.length) {
                if (i != j && strs[i].equals(strs[j])) {
                    ++i;
                    j = 0;
                    continue;
                } else if (i != j && !strs[i].equals(strs[j])) {
                    if (this.haveThis(strs[i], strs[j])) {
                        ++i;
                        j = 0;
                        continue;
                    } else {
                        ++j;
                    }
                } else {
                    ++j;
                }
                if (j > strs.length - 1 || strs[i].length() > strs[j].length()) {
                    return strs[i].length();
                }
            }
            return ret;

        }

        public boolean haveThis(String x, String y) {
            int j = 0;
            for (int i = 0; i < y.length() && j < x.length(); i++)
                if (x.charAt(j) == y.charAt(i))
                    j++;
            return j == x.length();
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}
